<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import axios from "axios";
let d1 = "";
let d2 = "";
let d3 = "";
let d4 = "";
let d5 = "";
let d6 = "";
let d7 = "";
export default {

  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '360px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },

  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    });
    // 发ajax请求，用以获取数据， 测试
    //正式
    //const url = 'http://8.135.121.102:8083/data/config/getDate';
    //测试
    const url = 'http://localhost:9000/data/config/getDate';

    axios.get(url).then(
        response => {
          const result = response.data.data;
          d1 = result.d1;//字符串按逗号分隔成数组
          d2 = result.d2;//字符串按逗号分隔成数组
          d3 = result.d3;//字符串按逗号分隔成数组
          d4 = result.d4;//字符串按逗号分隔成数组
          d5 = result.d5;//字符串按逗号分隔成数组
          d6 = result.d6;//字符串按逗号分隔成数组
          d7 = result.d7;//字符串按逗号分隔成数组
        }
    ).catch(
        response => {
          alert('系统繁忙!' + response);
        },
    );

  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },

    setOptions({ expectedData,titleText } = {}) {
      this.chart.setOption({
        title: {
          text: titleText,
          textStyle: {
            fontSize: 14,
            fontWeight: 'normal',
            color: '#5684CD',
          },
          x: 'center'
        },
        xAxis: {
          data: [d1,d2,d3,d4,d5,d6,d7],

          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [3, 6]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        /*legend: {
          data: ['用户访问量']
        },*/
        series: [{
          itemStyle: {
            normal: {
              label: {
                show: true, //开启显示
                position: 'top', //在上方显示
                textStyle: { //数值样式
                  color: '#5684CD',
                  fontSize: 10
                }
              },
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: expectedData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        }]
      })
    }
  }
}
</script>
